package com.tju.resource.tool.tool;

import java.lang.reflect.Constructor;
import java.util.Iterator;
import java.util.List;

/**
 * @Author HyEISN
 */
public class ListUtils {

    /**
     * Remove duplicate element in list, all element just one copy.
     * */
    public static <T extends List> void remDup(T list) {
        try {
            Constructor<? extends List> paramType = list.getClass().getConstructor();
            List copy = paramType.newInstance();
            List res = paramType.newInstance();
            Iterator copyIter;

            copy.addAll(list);
            copyIter = copy.iterator();
            list.clear();

            while (copyIter.hasNext()) {
                Object next = copyIter.next();

                copyIter.remove();

                if (!res.contains(next)) {
                    res.add(next);
                }
            }
            list.addAll(res);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
